package ru.lukyanets.transmission.protocol.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.io.Serial;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @author Sergey Lukyanets
 */
public class LocalDateTimeOfEpochDeserializer extends StdDeserializer<LocalDateTime> {
    @Serial
    private static final long serialVersionUID = 2089730855009378920L;

    protected LocalDateTimeOfEpochDeserializer() {
        super(LocalDateTime.class);
    }

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        long value = p.getLongValue();
        return value != 0 ? Instant.ofEpochSecond(value).atZone(ZoneId.systemDefault()).toLocalDateTime() : null;
    }
}
